# -*- coding: utf-8 -*-
# appengine imports
from google.appengine.ext import db
from google.appengine.api import memcache
from google.appengine.ext.webapp import template
from django.utils import simplejson as json

#python imports
import cgi, sys, random, hashlib, hmac, base64, os
#from datetime import datetime

#local imports
from baserequesthandler import BaseRequestHandler
from quizz.models import QAufgabe, Loesung
#from benutzer.models.schueler import Schueler, Besucher

#todo: memcache irgendwie einbauen?????

from einstellungen import APPLICATION_HOST


class Handler(BaseRequestHandler):
  def get(self,akey):
    loesung = Loesung.gql('where akey = :1 ', akey).get()
    bucket = self.request.get('bucket')
    if loesung is not None:
      if bucket == 'leonanachhilfe':
        loesung.video_hochgeladen = True
        loesung.put()

      host = self.request.headers['Host']      

      if not (host == APPLICATION_HOST):
        self.redirect('/')
        return

      template_values = {
        'akey' : akey,
        'html_tex': loesung.html_tex
      }
      directory = os.path.dirname(__file__).replace('/video','')
      path = os.path.join(directory, os.path.join('templates', 'video/lsg.html'))
      html = template.render(path, template_values)
      self.response.out.write(html)

      return

    else: # es gibt keine solche Quizz-Aufgabe, weg damit
      self.response.out.write(akey)
      #self.redirect('/')
      return

